$(document).ready(function () {
	var $btnSubmit = $('#userInfoForm button[type="submit"]').hide();
	var $rePasswordForm = $('#rePasswordForm').hide();
	var $btnChange = $('#userInfoForm button[type="button"]');
	var $input = $('#userInfoForm input');
	
	$btnChange.click(function () {
		$input.filter(function () {
			return $(this).attr('id')!='account' && $(this).attr('id')!='email';
		}).removeAttr('readonly');
		$rePasswordForm.fadeIn(200);
		$(this).fadeOut(200, function () {
			$btnSubmit.fadeIn(200);
		});
	});	
	
	$('#userInfoForm').submit(function (event) {
		event.preventDefault();
		
		var formValues = $(this).serialize();
		$.post('/users/changeInfo', formValues, function (result) {
			if(result.status == 0) {
				swal({
				  title: "修改失败",
				  text: "服务器错误，请稍后再试",
				  type: "error"
				});
			}else if(result.status == 1) {
				swal({
				  title: "修改成功",
				  type: "success"
				},function () {
					var herf = window.location.href;
					window.location.href = herf;
				});
			}
		});
	});
	
	$('#userPicture').on('change', function () {
		var data = new FormData();
		data.append('userPicture', $('#userPicture')[0].files[0]);
		
		$.ajax({
			type:"POST",
			url:"/users/userPicture",
			data: data,
			processData: false,
			contentType: false,
			success: function (result) {
				$picture = $('#picture');
				$picture.attr('src', result.data.uploadurl);
				$('#smallUserPicture').attr('src', result.data.uploadurl);
			}
		});
	});
	
	$('#picture').click(function () {
		$('#userPicture').click();
	});
});

